package com.youku.mson.serializer;

import java.util.List;

public class ListSerializer implements JsonSerializer {

	@Override
	public void serialize(JsonWriter out, Object target) {
		if (target == null) {
			out.writeNull();
		} else {
			out.beginArray();

			List<?> list = (List<?>) target;

			for (Object element : list) {

				JsonSerializer serializer = JsonSerializerFactory.getSerializer(element.getClass());
				serializer.serialize(out, element);
				out.writeComma();
			}

			if (out.endWith(",")) {
				out.deleteEnd();
			}

			out.endArray();
		}

	}

}
